package com.hyg.lib_base.mainUtils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

/**
 * 版本信息工具类
 *
 * @author: Administrator
 * @date: 2020/11/27
 */
public class PackageUtils {

    /**
     * description: 版本名称
     * @author visitant
     * @time 2020/11/27 14:39
     */
    public static String getVersionName(Context context) {
        return getPackageInfo(context).versionName;
    }

    /**
     * description:版本号
     * @author visitant
     * @time 2020/11/27 14:39
     */
    public static int getVersionCode(Context context) {
        return getPackageInfo(context).versionCode;
    }

    /**
     * description:获取版本info
     * @author visitant
     * @time 2020/11/27 14:39
     */
    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;
        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            Log.d("", "" + e.getMessage());
        }
        return pi;
    }

}
